<?php

namespace Admin\Controller;
use Think\Controller;

class OrderController extends BaseController{
	
	public function array_sort($array,$key){
		if(is_array($array)){
			$key_array = null;
			$new_array = null;
			for($i=0;$i<count($array);$i++){
				$key_array[$array[$i][$key]] = $i;
			}
			krsort($key_array);
			$j=0;
			foreach($key_array as $k=>$v){
				$new_array[$j] = $array[$v];
				$j++;
			}
			unset($key_array);
			return $new_array;
		}else{
			return $array;
		}
	}
	
	public function index(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}
		$nickname = I('username');
		$starttime = str_replace("+"," ",I('starttime'));
		$endtime = str_replace("+"," ",I('endtime'));
		$game = I('game');
		$is_add = I('is_add');
		if($nickname){
            $map['username'] = $nickname;
		}
		if ($game) {
			$map['game'] = $game;
		}
		if ( $is_add != '') {
			$map['is_add'] = $is_add;
		}


		$map['is_under'] = 1;
//		$map['t_id'] = '0';
		$order = M('order');
		$count = $order->where($map)->count();
        $map['starttime']=$starttime;
        $map['endtime']=$endtime;
//        $map['game'] = 'askldjsalij';
        $page = new \Think\Page($count,10,$map);
        unset($map['starttime']);
        unset($map['endtime']);
        if($starttime && $endtime){
            $start = strtotime($starttime);
            $end = strtotime($endtime);
            $map['time'] = array(array('egt',$start),array('elt',$end),'and');
        }
        $show = $page->show();
        $list1 = $order->where($map)->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();
        $list2 = $order->where($map)->order("id DESC")->select();

        for($i=0;$i<count($list1);$i++){
			if($list1[$i]['userid']){
				$list1[$i]['user'] = M('user')->where("id = {$list1[$i]['userid']}")->find();
			}else{
				$list1[$i]['user'] = M('user')->where("id = {$list1[$i]['uid']}")->find();
			}
		}

		$arr = array();
		$coin = 0;//盈亏
        $liusui=0;//流水
		foreach ($list2 as $k4=>$v4){
            $compare_points= $v4['add_points']-$v4['del_points'];
            $liusui+=$v4['del_points'];
            if (1==$v4['is_add']){

                $coin += $compare_points;
            }
        };
		foreach($list1 as $k3=>$v3){
			$come_points = 0;
			$out_points = 0;
			if($v3['game'] == 'hundred' || $v3['game'] == 'hundred2' || $v3['game'] == 'longhu'){
				$number = $list1[$k3]['number'];
				$hundred = M("hundred")->field("award_numbers")->where("period_number='$number'")->find();
				$odd_number_info = json_decode($v3['jincai'],true);
				$res_code = explode(',',$hundred['award_numbers']);
				$odd_numer = array_keys($odd_number_info);
				if(in_array($odd_numer[0],$res_code)){
					if($odd_numer[0] == '庄'){
						$come_points = $odd_number_info[$odd_numer[0]] * C('hundred_z_point')  - $odd_number_info[$odd_numer[0]];
					}else if($odd_numer[0] == '闲'){
						$come_points = $odd_number_info[$odd_numer[0]] * C('hundred_x_point')  - $odd_number_info[$odd_numer[0]];
					}else if($odd_numer[0] == '和'){
						$come_points = $odd_number_info[$odd_numer[0]] * C('hundred_h_point')  - $odd_number_info[$odd_numer[0]];
					}else if($odd_numer[0] == '庄对' || $odd_numer[0] == '闲对'){
						$come_points = $odd_number_info[$odd_numer[0]] * C('hundred_zxd_point')  - $odd_number_info[$odd_numer[0]];
					}
				}else{
					if($odd_numer[0] == '庄'){
						if(in_array('和',$res_code)){
							$come_points = 0;
						}else {
							$out_points = $odd_number_info[$odd_numer[0]];
						}
					}else if($odd_numer[0] == '闲'){
						if(in_array('和',$res_code)){
							$come_points = 0;
						}else {
							$out_points = $odd_number_info[$odd_numer[0]];
						}
					}else if($odd_numer[0] == '和'){
						$out_points = $odd_number_info[$odd_numer[0]];
					}else if($odd_numer[0] == '庄对' || $odd_numer[0] == '闲对'){
						$out_points = $odd_number_info[$odd_numer[0]];
					}
				}
				$arr['add_points'] = $come_points;
				$arr['del_points'] = $out_points;
			}else{
				$ok = $v3['add_points']-$v3['del_points'];
				if($ok>0){
					$arr['add_points'] = $ok;
					$arr['del_points'] = 0;
				}else{
					$arr['add_points'] = 0;
					$arr['del_points'] = $v3['del_points'];
				}
			}
			$list1[$k3]['compare_points'] = $v3['add_points']-$v3['del_points'];
//			$coin += $list1[$k3]['compare_points'];
		}

		$this->assign('list',$list1);
		$this->assign('show',$show);
		$this->assign('username',$nickname);
		$this->assign('game',$game);
		$this->assign('is_add',$is_add);
		$this->assign('starttime',$starttime);
        $this->assign('endtime',$endtime);
        $this->assign('coin',$coin);
        $this->assign('liusui',$liusui);
		$this->display();
	}

	public function del(){
		$id = I('id');
		if($id){
			$order = M('order')->where("id={$id}")->find();
			$res = M('order')->where("id = $id")->delete();
			M('user')->where("id = {$order['t_id']}")->setDec('yong',$order['del_points']*C('fenxiao')*0.01);
			M('user')->where("id = {$order['t_id']}")->setDec('t_add', $order['del_points']*C('fenxiao')*0.01);
			M('push_money')->where("orderid={$id}")->delete();
			
			if($res){
				$this->success('删除成功！');
			}else{
				$this->error('删除失败！');
			}
		}
	}

	public function del_order(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确！');
			}else{
				$starttime = I('starttime');
				$endtime = I('endtime');
				if (!$starttime || !$endtime) {
					$this->error("请选择时间");
				}

				$map['time'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)),'and');
				$res = M('order')->where($map)->delete();
				if ($res) {
					$this->success('删除成功!',U('Order/index'),1);
				} else {
					$this->error('删除失败');
				}
			}
		} else {
			$this->display();
		}
	}



	public function admin_cancel(){
		$id = I('id');
		if($id){
			$order = M('order')->where("id={$id}")->find();
			if ($order['add_points']) {
				$add = $order['add_points']-$order['del_points'];
				$res = M('user')->where("id = {$order['userid']}")->setDec('points',$add);
				$res1 = M('order')->where("id = $id")->setField(array('is_add'=>'1','del_points'=>0,'add_points'=>0));
			} else{
				$points = $order['del_points'];
				$res = M('user')->where("id = {$order['userid']}")->setInc('points',$points);
				$res1 = M('order')->where("id = $id")->setField(array('is_add'=>'1','del_points'=>0,'add_points'=>0));
			}
		
			if($res && $res1){
				$this->success('取消成功');
			} else {
				$this->error('取消失败！');
			}
		}
	}
	
	//每日输赢
	public function win_lose(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		$map['state'] = 1;

		$time = I('time');
		$start = strtotime(str_replace("+"," ",I('starttime'))) ? : mktime(0,0,0,date('m'),date('d'),date('Y'));
		$end = strtotime(str_replace("+"," ",I('endtime')))  ? : mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		
		if($time){
			// $start = strtotime($time.'00:00:00');
			// $end = strtotime($time.'23:59:59');
			if ($time == 'today') {
				$start=mktime(0,0,0,date('m'),date('d'),date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			} elseif($time == 'yestoday') {
				$start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
			} elseif($time == 'week') {
				$start=mktime(0,0,0,date('m'),date('d')-6,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			}  elseif($time == 'month') {
				$start=mktime(0,0,0,date('m'),date('d')-29,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			}
		}

		$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		$order = M('order');
		$list = $order->field("SUM(add_points) AS add_points,SUM(del_points) AS del_points")->where($map)->find();
	
		$this->assign('nickname',$nickname);
		$this->assign('del',$list['del_points']);
		$this->assign('add',$list['add_points']) ;
		$this->assign('ying',$list['del_points']-$list['add_points']);
		$this->assign('start',date('Y-m-d H:i:s',$start));
		$this->assign('end',date('Y-m-d H:i:s',$end));
		$this->display();
	}
	
	
	//用户每日输赢
	public function user_win_lose(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		$nickname = I('username');
		$time = I('time');
		$map = array();
		if($nickname){
            $map['username'] = $nickname;
		}

		$map['state'] = 1;
		$time = I('time');
		$start = strtotime(str_replace("+"," ",I('starttime'))) ? : mktime(0,0,0,date('m'),date('d'),date('Y'));
		$end = strtotime(str_replace("+"," ",I('endtime'))) ? : mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		
		if($time){//时间区间条件
			if ($time == 'today') {
				$start=mktime(0,0,0,date('m'),date('d'),date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			} elseif($time == 'yestoday') {
				$start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
			} elseif($time == 'week') {
				$start=mktime(0,0,0,date('m'),date('d')-6,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			}  elseif($time == 'month') {
				$start=mktime(0,0,0,date('m'),date('d')-29,date('Y'));
				$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			}
		}

		$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		$order = M('order');

		$count = $order->where($map)->group('userid')->select();
		$page = new \Think\Page(count($count),50,$map);
		$show = $page->show();
//        user_liusui2(0,$map);
		$user_list = M('user')->where($map)->field('id,t_id,path,is_agent,nickname,username,liushui')->order("liushui DESC")->select();

		$arr = array();
		foreach ($user_list as $k=>$v){
			$arr[$k]['add_points'] = 0;
			$arr[$k]['del_points'] = 0;
			$arr[$k]['compare_points'] = 0;
			//查询该会员所有投注
			$map['userid'] = $v['id'];
			$map['is_add'] = 1;

            $order_member = M("order")->where($map)->select();
            $ls=0;//用户总流水

            foreach($order_member as $k3=>$v3){
                $ok = $v3['add_points']-$v3['del_points'];
                if($ok>0){
                    $arr[$k]['add_points'] += $ok;
                }else{
                    if (1==$v3['is_add']){
                        $arr[$k]['del_points'] += $ok*-1;
                    }
                }
                $ls+=$v3['del_points'];
			}
			$arr[$k]['ying'] = $arr[$k]['add_points']-$arr[$k]['del_points'];
			$arr[$k]['nickname'] = $v['nickname'];
			$arr[$k]['headimgurl'] = $v['headimgurl'];
			$arr[$k]['t_id'] = $v['t_id'];
			$arr[$k]['remark'] = $v['remark'];
			$arr[$k]['username'] = $v['username'];
            //删除流水为0的用户
            if ($ls==0){
                unset($arr[$k]);
            }
		}
		$this->assign('nickname',$nickname);
		$this->assign('list',$arr);
		$this->assign('time',$time);
		$this->assign('show',$show);
		$this->assign('start',date('Y-m-d H:i:s',$start));
		$this->assign('end',date('Y-m-d H:i:s',$end));
		$this->display();
	}
}


?>